package com.linjiahao.nettyGateway.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.linjiahao.commonUtil.Redis.RedisService;
import com.linjiahao.nettyGateway.constant.Constant;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @ClassName MessageConsumer
 * @Description: TODO
 * @Author linjiahao
 * @Date 2023/1/18
 **/
@Component
public class MessageConsumer {

    @Resource
    private RedisService redisService;

    @SneakyThrows
    @RabbitListener(queues = "${spring.rabbitmq.message-send.queue}")
    @RabbitHandler
    public void process(Map map, Channel channel, Message message){
        System.out.println("receiver: "+ JSON.toJSONString(map));
        String userId = String.valueOf(map.get("userId"));
        if(redisService.hHasKey(Constant.USER_CONNECT_KEY, userId)){
            Object o = redisService.hget(Constant.USER_CONNECT_KEY, userId);
            System.out.println(o.toString());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }

}
